package com.young.android.view;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.young.android.R;
import com.young.android.common.Const;
import com.young.android.entity.Item;

public class ItemListAdapter extends BaseAdapter {

    private ArrayList<Item> itemList;
    private Context context;

    public ItemListAdapter(Context context, ArrayList<Item> itemList) {
        this.itemList = itemList;
        this.context = context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return itemList.size();
    }

    @Override
    public Item getItem(int position) {
        // TODO Auto-generated method stub
        return itemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflate = ((Activity) context).getLayoutInflater();
        View view = (View) inflate.inflate(R.layout.item_list, null);
        Item r = itemList.get(position);
        int state = r.state;
        ImageView stateImg = (ImageView) view.findViewById(R.id.item_state_img);
        if (state == Const.ITEM_READ) {
            stateImg.setBackgroundDrawable(view.getResources().getDrawable(R.drawable.read_icon));
        } else {
            stateImg.setBackgroundDrawable(view.getResources().getDrawable(R.drawable.unread_icon));
        }
        TextView textView = (TextView) view.findViewById(R.id.item_list_title);
        textView.setText(itemList.get(position).title);
        textView = (TextView) view.findViewById(R.id.item_list_des);
        textView.setText(itemList.get(position).description);
        textView = (TextView) view.findViewById(R.id.item_list_pubdate);
        textView.setText(itemList.get(position).date);
        return view;
    }

}
